<?php 
/**
 * @description : success page for third payment of h3g
 * @author      : stephen.mo
 * @date        : Nov 11,2010
 */
require_once "TVB/OAuth/common.inc.php";
#ini_set("display_errors","on");

$oauth_callback  = (isset($_REQUEST['oauth_callback'])) ? $_REQUEST['oauth_callback'] : NULL;
$tvbauth         = (isset($_REQUEST['tvbauth'])) ? $_REQUEST['tvbauth'] : NULL;
$msisdn          = (isset($_REQUEST['msisdn'])) ? $_REQUEST['msisdn'] : NULL;
$base_server_path  = OAuthConfig::OAuthDomainUrl($_SERVER);
$message_url       = $base_server_path.OAuthConfig::DEFAULT_SHOW_MESSAGE_URL;

//all parameter not allow null.
if (empty($oauth_callback) || empty($msisdn)) {
    $retry = (!empty($oauth_callback)) ? $oauth_callback : '#';
    $other_msg = array('call_back'=>urlencode($retry));
    OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_ERROR,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1035,1037),$other_msg);
    exit;
}

//to analyze a url and get some necessary parameter.
$param_vars = array();
if (!empty($oauth_callback)) {
    $query_param_vars  = parse_url($oauth_callback);
    if (isset($query_param_vars['query'])) {
        parse_str($query_param_vars['query'],$param_vars);
    }
}

//param setting.
$lang              = (isset($param_vars['lang'])) ? $param_vars['lang'] : 'zh-hant-hk';
$call_back         = (isset($param_vars['call_back'])) ? $param_vars['call_back'] : $oauth_callback;
$network_type      = (isset($param_vars['ntype'])) ? $param_vars['ntype'] : NULL;

//verify the oauth token.
if (OAuthModelConfig::IS_NEED_CHECK_OAUTHTOKEN) {
    $access_token = $param_vars['oauth_token'];
    $ret = OAuthUtils::verifyAccessToken($access_token);
    if (!$ret || !$ret['flag']) {
        $other_msg = array('call_back'=>(isset($oauth_callback) ? urlencode($oauth_callback) : '#' ) );
        OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_ERROR,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1005,1006),$other_msg);
        exit;
    }
}

//todo succBack
$rec = OAuthUtils::decryptMsisdn($msisdn);
$msisdn_str = (isset($rec['msisdn_str'])) ? $rec['msisdn_str'] : NULL;
if (empty($rec) || empty($msisdn_str)) {
    $msgbox_url = $error_url.'&call_back='.urlencode($call_back).'&errids='.json_encode(array(1030,1035,1041));
    OAuthUtils::redirectTo($msgbox_url);
    exit;
}

//call success back API.
$ret = OAuthUtils::h3gPaymentSuccBack($tvbauth,$msisdn_str,$network_type);
if (empty($ret)) {
    $other_msg = array('call_back'=>urlencode($oauth_callback));
    OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_ERROR,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1041),$other_msg);
    exit;
}

//redirect to a static html page.
$message_url = $base_server_path.OAuthConfig::DEFAULT_SHOW_MESSAGE_URL.'?act=request&flag='.OAuthModelConfig::DEFAULT_CONST_SUCCESS.'&lang='.$lang;
OAuthUtils::redirectTo($message_url);
exit;

?>
